package com.znz.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.znz.util.common.Page;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

import javax.persistence.*;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: johnson
 * Date: 5/6/15
 * Time: 2:28 下午
 */
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", identifierColumn = "id", identifierField = "id", table = "t_hospital")
public class Thospital {

	//医院名称
	@Column(name = "hospital_name")
	private String hospitalName;
	//	医院别名
	@Column(name = "hospital_alias")
	private String hospitalAlias;
	//	联系电话
    @Column(name = "phone")
	private String phone;
	//	省份
    @Column(name = "province")
	private String province;
	//	城市
    @Column(name = "city")
	private String city;
	//	医院地址
	@Column(name = "hospital_address")
	private String hospitalAddress;
	//	医院属性
	@Column(name = "hospital_nature")
	private int hospitalNature;
	//	项目级别
	@Column(name = "project_level")
	private int projectLevel;
	//	医院网址
	@Column(name = "hospital_url")
	private String hospitalUrl;
	//	特色项目
	@Column(name = "features_project")
	private String featuresProject;
	@Column(name = "contract_deadline")
	//	合同到期日期
	private Date contractDeadline;
	//状态 -1:删除 0:新建待审核 1:审核通过
    @Column(name = "status")
	private int status;
	//	资质证明照片路径
    @Column(name = "certification")
	private String certification;
	//	0:不是vip 1:vip
    @Column(name = "vip_flag")
	private int vipFlag;
    @Column(name = "create_time")
	private Date createTime;
    @Column(name = "update_time")
	private Date updateTime;
	//联系人信息 json存储
    @Column(name = "contacts")
	private String contacts;

    @OneToMany(mappedBy = "hospitalId",cascade= CascadeType.ALL)
    @JsonIgnore
    private List<ThospitalProjectAgreement> thospitalProjectAgreementList;


    public static List<Thospital> getHospitalByWhere(String where,List<Object> params,int firstResult,int maxResults)throws Exception{
        StringBuilder strSql=new StringBuilder();
        strSql.append("SELECT o FROM Thospital WHERE o.status!=-1 ");
        strSql.append(where);
        TypedQuery<Thospital> query=entityManager().createQuery(strSql.toString(),Thospital.class).setFirstResult(firstResult).setMaxResults(maxResults);
        if (null!=params && params.size()>0){
            int i=0;
            for (Object key :params){
                query.setParameter(i,"%"+key+"%");
                i++;
            }
            return query.getResultList();
        }
        return  query.getResultList();
    }

    /**
     * 方法说明:查看所有注册但未审核的医院列表
     * @param where
     * @param params
     * @param firstResult
     * @param maxResults
     * @return
     * @throws Exception
     */
    public static List<Thospital> getunApproveHospitalList(String where,List<Object> params,int firstResult,int maxResults)throws Exception{
        StringBuilder strSql=new StringBuilder();
        strSql.append("SELECT o FROM Thospital WHERE o.status=0 ");
        strSql.append(where);
        TypedQuery<Thospital> query=entityManager().createQuery(strSql.toString(),Thospital.class).setFirstResult(firstResult).setMaxResults(maxResults);
        if (null!=params && params.size()>0){
            int i=0;
            for (Object key :params){
                query.setParameter(i,"%"+key+"%");
                i++;
            }
            return query.getResultList();
        }
        return  query.getResultList();
    }
    public static List<Thospital> getHospitalAllByCityId(String where,Map<String,Object> params,int firstResult,int maxResults)throws Exception{
        StringBuilder strSql=new StringBuilder();
        strSql.append("SELECT * FROM t_hospital o WHERE o.status!=-1 ");
        strSql.append(where);
        Query query=entityManager().createNativeQuery(strSql.toString(),Thospital.class).setFirstResult(firstResult).setMaxResults(maxResults);
        if (null!=params && params.size()>0){
           Set<String> keyset=params.keySet();
           for (String key:keyset){
               query.setParameter(key,params.get(key));
           }
          return  query.getResultList();
        }
        return  query.getResultList();
    }

    public static List<Thospital> getHospitalAllByCityIdBytest(String where,Map<String,Object> params)throws Exception{
        StringBuilder strSql=new StringBuilder();
        strSql.append("SELECT * FROM t_hospital o WHERE o.status!=-1 ");
        strSql.append(where);
        Query query=entityManager().createNativeQuery(strSql.toString(), Thospital.class);
        if (null!=params && params.size()>0){
            Set<String> keyset=params.keySet();
            for (String key:keyset){
                query.setParameter(key,params.get(key));
            }
            return  query.getResultList();
        }
        return  query.getResultList();
    }
    public static boolean approveHospital(Long hospitalId){
        String sql="update set status=1 where id=? ";
        Query query=entityManager().createNativeQuery(sql);
        query.setParameter(1,hospitalId);
        int i=query.executeUpdate();
        if(i>0){
            return true;
        }
        return false;
    }

}

